import type { Router } from 'vue-router';
import { setRouteEmitter } from '@/settings/utils/route-listener';
import setupUserLoginInfoGuard from './user-login-info';
import setupPermissionGuard from './permission';
import setupCommunityInfoGuard from './community-info';
import { useAppStore } from '@/settings/stores';

function setupPageGuard(router: Router) {
  router.beforeEach((to) => {
    // emit route change
    setRouteEmitter(to);
  });

  router.afterEach((to) => {
    const app = useAppStore();

    setTimeout(() => {
      app.setWindowTitle(to.meta.autoSetTitle !== false ? to.meta.title : '');
    });
  });
}

export default function createRouteGuard(router: Router) {
  setupPageGuard(router);
  setupCommunityInfoGuard(router);
  setupUserLoginInfoGuard(router);
  setupPermissionGuard(router);
}
